package com.wch.clientdemo.impl.module.home;

import java.util.List;

import com.wch.clientdemo.R;
import com.wch.clientdemo.module.base.adapters.HomeFragmentAdapter;
import com.wch.clientdemo.module.base.config.MenuModule;
import com.wch.clientdemo.module.base.config.MyApplication;
import com.wch.clientdemo.module.base.config.MyConfig;
import com.wch.clientdemo.module.base.ui.NoScrollGridView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

/**
 * app主界面
 * @author 19821
 *
 */
public class HomeActivity  extends Activity implements OnClickListener{

	public DrawerLayout drawerLayout;
	/**
	 * 顶部标题视图
	 */
	private TextView mHomeTitleTv;
	/**
	 * 主页菜单网格视图
	 */
	private NoScrollGridView mNoScrollGridView = null;
	
	/**
	 * 主页适配器
	 */
	private HomeFragmentAdapter homeFragmentAdapter = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_home_layout);
		initData();
		initView();
		initEvent();
	}

	/**
	 * 对事件初始化
	 */
	private void initEvent() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 初始化界面
	 */
	private void initView() {
		drawerLayout =  (DrawerLayout) findViewById(R.id.home_drawer_layout);
		//顶部导航栏
		 ViewGroup homeTop = (ViewGroup) this.findViewById(R.id.home_top);
		 TextView mHomeTitleTv = (TextView) homeTop.findViewById(R.id.home_title_tv);
		 mHomeTitleTv.setText("测试app");
		 
		 //主页容器
		 ViewGroup homeBaseContainer = (ViewGroup) findViewById(R.id.home_base_container);
		 View homeLayouot = View.inflate(this, R.layout.fragment_home_layout,null);
		 initHomePageModule(homeLayouot);
		 homeBaseContainer.addView(homeLayouot);
	}

	/**
	 * 初始化菜单主页模块
	 * @param homeLayouot
	 */
	private void initHomePageModule(View homeLayouot) {
		// TODO Auto-generated method stub
		mNoScrollGridView = (NoScrollGridView) homeLayouot.findViewById(R.id.gridview);
		homeFragmentAdapter = new HomeFragmentAdapter(this);
		setAdapterData(homeFragmentAdapter);
		mNoScrollGridView.setAdapter(homeFragmentAdapter);
		mNoScrollGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Object item = homeFragmentAdapter.getItem(position);
				if (item instanceof MenuModule) {
					try {
						gotoPage((MenuModule)item,null);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
			}
		});
		
		
	}

	/**
	 * 根据模块路径跳转到对应路径模块
	 * @param item
	 * @param intent
	 */
	protected void gotoPage(MenuModule item, Intent intent) {
		String className = item.className;
		if(null == intent){
			intent = new Intent();
		}
		
		if(className.contains("/")){
			return;
		}else{
			intent.setClassName(this, className);
			startActivity(intent);
		}
		
		
	}

	/**
	 * 适配器添加数据
	 * @param homeFragmentAdapter2
	 */
	private void setAdapterData(HomeFragmentAdapter adapter) {
		//通过全局配置文件读取菜单配置文件
		List<MenuModule> menuList = getConfig().menuList;
		if(null == menuList){
			return;
		}
		
		//加载数据,先清空数据再加载
		adapter.clear();
		for(int i=0;i<menuList.size();i++){
			MenuModule node = menuList.get(i);
			adapter.add(node);
		}
		
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 获取全局配置文件
	 * @return
	 */
	public MyConfig getConfig(){
		MyApplication app = (MyApplication) getApplication();
		return app.getMyConfig();		
	}

	
}
